package com.tmeone.erpone.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasterxml.jackson.databind.annotation.JacksonStdImpl;
import com.tmeone.erpone.entity.JsonResult;
import com.tmeone.erpone.entity.TIoWarehouse;
import com.tmeone.erpone.entity.TIomWarehouse;
import com.tmeone.erpone.service.ITIoWarehouseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author tmeone
 * @since 2021-04-20
 */
@RestController
public class TIoWarehouseController {

    @Autowired
    private ITIoWarehouseService warehouseService;
    //出入库分页
    @GetMapping("/iaos")
    public JsonResult getPageInAndOut(@RequestParam(value = "current",required = false,defaultValue = "1") Integer current,
                                      @RequestParam(value ="pageSize",required = false,defaultValue = "5") Integer pageSize){
        Page<TIoWarehouse> page = new Page<>(current, pageSize);
        IPage<TIoWarehouse> iaos = warehouseService.pageInAndOut(page);
        return new JsonResult(1,iaos);
    }
    /*//查询所有的出入库
    @RequestMapping(value="/iaos",method = RequestMethod.GET)
    public JsonResult getIAOs(){
        List<TIoWarehouse> list = warehouseService.list();
        System.out.println(list);
        return new JsonResult(1,list);
    }*/

    //根据出入库单号查询详情
    @RequestMapping("/iaos/{ioNo}")
    public JsonResult getDetailsInAndOut(@PathVariable String ioNo){
        List<TIomWarehouse> details = warehouseService.getDetailsInAndOutByIomNo(ioNo);
        return new JsonResult(1,details);
    }

    //出入库添加
    @PostMapping("/iaos")
    public JsonResult saveINAndOut(@RequestBody TIoWarehouse warehouse){
        warehouse.setIoTime(LocalDate.now());
        warehouse.setId(UUID.randomUUID().toString().replace("-","").substring(0,10));
        warehouseService.save(warehouse);
        System.out.println(warehouse);
        return new JsonResult(1,warehouse);
    }

    //查询出入库类型
    @GetMapping("/ioTypes")
    public JsonResult selectIoType(){
        List<String> list = warehouseService.selectIoType();
        return new JsonResult(1,list);
    }

    //查询仓库名称
    @GetMapping("/wName")
    public JsonResult selectWName(){
        List<String> list = warehouseService.selectWName();
        return new JsonResult(1,list);
    }

    //根据出入库单号和批次查询数据
    @GetMapping("/select/{ioNo}/{batch}")
    public JsonResult selectOneByIoNoAndBatch(@PathVariable String ioNo,@PathVariable int batch){
        List<TIoWarehouse> list = warehouseService.selectOne(ioNo, batch);
        return new JsonResult(1,list);
    }

}
